Homework 4¶

Meredith Palmore¶

Date: 2/21/2022¶

Import libraries and dataset

In [1]:
import pandas as pd
import plotly.express as px
import numpy as np
import plotly.graph_objects as go

df = pd.read_csv("https://raw.githubusercontent.com/smart-stats/ds4bio_book/main/book/assetts/kirby21AllLevels.csv")
df = pd.DataFrame(df)

df.head(5)

subjofint = df.loc[(df.rawid == "kirby906a_ax.img")]

Question 1: Look at the chapter on interactive graphics and, specifically, the code to display a subject's MRICloud data as a sunburst plot. Do the following. Display this subject's data as a Sankey diagram. Display as many levels as you can for type = 1, starting from the intracranial volume. Put this in a file called hw4.ipynb.¶

In [2]:
print(subjofint)

summary_measures = subjofint[["type", "level","roi","volume"]].groupby(["type", "level","roi"], as_index = False).sum()
                   
type1 = summary_measures.loc[(summary_measures.type == 1)]
print(type1)
       Unnamed: 0             rawid              roi  volume    min    max  \
12540       12541  kirby906a_ax.img  Telencephalon_L  467063    2.0  350.0   
12541       12542  kirby906a_ax.img  Telencephalon_R  470488    2.0  337.0   
12542       12543  kirby906a_ax.img   Diencephalon_L    8801   60.0  327.0   
12543       12544  kirby906a_ax.img   Diencephalon_R    9054   63.0  415.0   
12544       12545  kirby906a_ax.img    Mesencephalon    9564   86.0  352.0   
...           ...               ...              ...     ...    ...    ...   
13371       13372  kirby906a_ax.img   Caudate_tail_L     424  112.0  279.0   
13372       13373  kirby906a_ax.img   Caudate_tail_R     386   83.0  286.0   
13373       13374  kirby906a_ax.img   Chroid_LVetc_L     101   56.0  255.0   
13374       13375  kirby906a_ax.img   Chroid_LVetc_R      84   53.0  271.0   
13375       13376  kirby906a_ax.img     IV_ventricle    1835    3.0  275.0   

           mean      std  type  level   id      icv      tbv  
12540  165.2599  57.1707     1      1  906  1195015  1123076  
12541  171.8695  59.3001     1      1  906  1195015  1123076  
12542  227.1878  31.2303     1      1  906  1195015  1123076  
12543  231.6770  31.1780     1      1  906  1195015  1123076  
12544  269.1003  28.6454     1      1  906  1195015  1123076  
...         ...      ...   ...    ...  ...      ...      ...  
13371  182.8215  31.9975     2      5  906  1195015  1123076  
13372  186.3707  37.6639     2      5  906  1195015  1123076  
13373  181.6190  39.8132     2      5  906  1195015  1123076  
13374  181.9857  43.3901     2      5  906  1195015  1123076  
13375  108.3120  53.6962     2      5  906  1195015  1123076  

[836 rows x 13 columns]
     type  level                roi  volume
0       1      1                CSF   71939
1       1      1     Diencephalon_L    8801
2       1      1     Diencephalon_R    9054
3       1      1      Mesencephalon    9564
4       1      1      Metencephalon  154071
..    ...    ...                ...     ...
486     1      5  subcallosal_ACC_R     484
487     1      5  subgenualWM_ACC_L     218
488     1      5  subgenualWM_ACC_R     106
489     1      5    subgenual_ACC_L    1404
490     1      5    subgenual_ACC_R    1233

[491 rows x 4 columns]
In [3]:
regions = type1.roi[0:11].tolist()

print(regions)
['CSF', 'Diencephalon_L', 'Diencephalon_R', 'Mesencephalon', 'Metencephalon', 'Myelencephalon', 'Telencephalon_L', 'Telencephalon_R', 'BasalForebrain_L', 'BasalForebrain_R', 'CerebralCortex_L']
In [4]:
icv = [sum(subjofint.volume)]

san_valuelist = icv + type1.volume[0:11].tolist()

san_labels = ["icv"] + type1.roi[0:11].tolist()

print(san_valuelist)
print(san_labels)


fig = go.Figure(data=[go.Sankey(
     node = dict(
       pad = 15,
       thickness = 20,
       line = dict(color = "black", width = 0.5),
       label = san_labels,
       color = "blue"
     ),
     link = dict(
       source = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # indices correspond to labels, eg A1, A2, A1, B1, ...
       target = [1,2,3,4,5,6,7,8,9,10,11,12],
       value = san_valuelist
   ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show(engine = "collab")
[11950461, 71939, 8801, 9054, 9564, 154071, 4035, 467063, 470488, 2686, 2609, 246947]
['icv', 'CSF', 'Diencephalon_L', 'Diencephalon_R', 'Mesencephalon', 'Metencephalon', 'Myelencephalon', 'Telencephalon_L', 'Telencephalon_R', 'BasalForebrain_L', 'BasalForebrain_R', 'CerebralCortex_L']
In [5]:
! jupyter nbconvert --to html hw4.ipynb
[NbConvertApp] Converting notebook hw4.ipynb to html
[NbConvertApp] Writing 4265239 bytes to hw4.html

https://github.com/mpalmore/hw4_repo_ds4ph